package sysUtil

import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
import xdTool.XdTableColCfg
import xdTool.XdWidgetSelect

/**
 * Created by Administrator on 2019-03-19.
 * 解析页面dom
 */
class JsoupUtil {

    /**
     * xxd ，根据table-field绑定的select，生成填报使用的select组件
     * @param xdTableColId
     */
    static getTianBaoBingdingHtml(xdTableColId){
        def select = ""
        try {
            XdTableColCfg xdTableColCfg = XdTableColCfg.get(Long.parseLong(String.valueOf(xdTableColId)))
            if(xdTableColCfg.paramId){
                XdWidgetSelect xdWidgetSelect = XdWidgetSelect.get(xdTableColCfg.paramId)
                if(xdWidgetSelect){
                    def element = JsoupUtil.getJsoupInstanceFromDom(SysCommUtil.Clob2String(xdWidgetSelect.html))
                    select = element.select("select")
                    if(select.size()>=1){
                        select = select.get(0)
                        select.attr("name",xdTableColCfg.field)
                        select.attr("id",xdTableColCfg.field)
                        //select.attr("xm-select",xdTableColCfg.field)
                        select = select.outerHtml()
                    }
                }
            }
        }catch (Exception e){
            println "生成填报组件：select 异常.............."
            select = ""
            e.printStackTrace()
        }
        return String.valueOf(select)
    }

    /**
     * 根据dom-html生成jsoup实例
     * @param dom
     * @return
     */
    static getJsoupInstanceFromDom(dom){
        Document doc = Jsoup.parseBodyFragment(String.valueOf(dom));
        Element element =  doc.body()
        return element
    }
}
